﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using ListenMusic.AdminAlbum_ServiceReference;
using ListenMusic.Models.Reposistory.Public;
using ListenMusic.Models.Reposistory.User;
using System.Transactions;
using System.IO;
using ListenMusic.Singer_ServiceReference;
using ListenMusic.Musician_ServiceReference;
namespace ListenMusic.Models.Reposistory.Admin
{
    public class AdminUploadReposistory
    {        
        UploadResposistory _upload;
        MusicianReposistory _musician;
        SingerReposistory _singer;
        ListenMusic.Models.Reposistory.User.SongReposistory _songUser;
        ListenMusic.Models.Reposistory.Admin.ManageAlbumReposistory _albumAdmin;
        ListenMusic.Models.Reposistory.Admin.ManageSingerReposistory _albumSinger;
        public AdminUploadReposistory()
        {
            _upload = new UploadResposistory();
            _musician = new MusicianReposistory();
            _singer = new SingerReposistory();
            _songUser = new Models.Reposistory.User.SongReposistory();
            _albumAdmin = new ListenMusic.Models.Reposistory.Admin.ManageAlbumReposistory();
            _albumSinger = new ManageSingerReposistory();
        }
       public bool ConfirmUploadAlbum(IEnumerable<HttpPostedFileBase> filename, string albumname, string singername, int country, int category, HttpPostedFileBase filepicture, string description, string releaseyear, string pathSong, string pathAlbumImage)
        {
            
            try
            {
                if (filename != null)
                {
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        try
                        {
                            int maxSongID = _songUser.getMaxID_SongID();
                            int maxAlbumSongID = _albumAdmin.getMaxID_AlbumSongID();
                            int maxSingerID = _singer.getMaxID_Singer();
                            int i = 0;                           
                            string randomStr = _upload.RandomStr();
                            AdminAlbum_ServiceReference.Album album = new AdminAlbum_ServiceReference.Album();
                            int newAlbumID = _albumAdmin.getMax_AlbumID() + 1;
                            album.AlbumID = newAlbumID;
                            album.AlbumName = albumname;
                            album.CategoryID = category;
                            album.CountryID = Convert.ToInt16(country);
                            album.CountView = 0;
                            album.ReleaseYear = Convert.ToInt16(releaseyear);
                            album.DateUp = DateTime.Now;
                            if (singername == "")
                            {
                                album.SingerID = 21;
                            }
                            else
                            {
                                ListenMusic.Singer_ServiceReference.Singer singer = _singer.checkSingerName(singername);
                                if (singer != null)
                                {
                                    album.SingerID = singer.SingerID;
                                }
                                else
                                {
                                    Singer newSinger = new Singer();
                                    newSinger.SingerName = singername;
                                    newSinger.Display = false;
                                    newSinger.CountryID = Convert.ToInt16(country);
                                    newSinger.Picture = "no-image.gif";
                                    bool rs = _singer.insertNewSinger(newSinger);
                                    if (rs)
                                    {
                                        album.SingerID = maxSingerID + 1;
                                    }
                                    else
                                    {                                        
                                        ts.Dispose();
                                        return false;
                                    }
                                }
                            }
                            //get name picture album
                            if (filepicture == null)
                            {
                                album.Picture = "no-image.gif";
                            }
                            else
                            {
                                string pictureName = Path.GetFileName(filepicture.FileName).ToLower().Replace(" ", "-");
                                string pictureURL = randomStr + "-" + pictureName;
                                string pathPicture = pathAlbumImage + pictureURL;
                                filepicture.SaveAs(pathPicture);
                                album.Picture = pictureURL;
                            }
                            bool resultAlbum = _albumAdmin.insertNewAlbum(album);
                            if (resultAlbum)
                            {
                                foreach (var file in filename)
                                {
                                    i++;
                                    string songNameNoExtension = Path.GetFileNameWithoutExtension(file.FileName);
                                    string extension = Path.GetExtension(file.FileName);
                                    string songURL = randomStr + "-" + songNameNoExtension.ToLower().Replace(" ", "-") + extension;
                                    AdminAlbum_ServiceReference.Album_Song albumSong = new AdminAlbum_ServiceReference.Album_Song();
                                    //end get name picture album                                                                            
                                    User_Song_ServiceReference.Song song = new User_Song_ServiceReference.Song();
                                    song.SongID = maxSongID + i;
                                    song.SongName = songNameNoExtension;
                                    song.Active = false;
                                    song.SongURL = songURL;
                                    song.MusicianID = 35;
                                    song.Lyrics = null;
                                    song.CountView = 0;
                                    if (singername == "")
                                    {
                                        song.SingerID = 21;
                                    }
                                    else
                                    {
                                        song.SingerID = maxSingerID + 1;
                                    }
                                    song.DateUp = DateTime.Now;
                                    song.UserName = "vampire89";
                                    song.CountryID = Convert.ToInt16(country);
                                    song.CategoryID = category;
                                    song.TypeID = 1;
                                    bool resultSong = _upload.uploadSong(song);
                                    if (resultSong)
                                    {
                                        albumSong.SongID = maxSongID + i;
                                    }
                                    else
                                    {
                                        ts.Dispose();
                                        return false;
                                    }
                                    albumSong.AlbumID = newAlbumID;
                                    albumSong.Album_SongID = maxAlbumSongID + i;
                                    bool resultAlbumSong = _albumAdmin.uploadAlbumSong(albumSong);
                                    if (resultAlbumSong)
                                    {
                                        string paths = pathSong + songURL;
                                        file.SaveAs(paths);
                                    }
                                    else
                                    {
                                        ts.Dispose();
                                        return false;
                                    }
                                }                               
                            }
                            else
                            {
                                ts.Dispose();
                                return false;
                            }
                            ts.Complete();
                        }
                        catch (Exception ex)
                        {                            
                            ts.Dispose();
                            return false;
                            throw ex;
                        }
                    }
                }
                return true;
            }
            catch (FaultException ex)
            {
                return false;
                throw ex;
            }
        }
       public bool ConfirmUploadSong(IEnumerable<HttpPostedFileBase> filename, string songname, string lyrics, string singername, string musicianname, int country, int category, string pathSong, string pathVideo)
       {           
           try
           {
               if (filename != null)
               {
                   using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                   {
                       try
                       {
                           int maxSongID = _songUser.getMaxID_SongID();
                           int maxMusician = _musician.getMaxID_Musician();
                           int maxSingerID = _singer.getMaxID_Singer();
                           int i = 0;
                           int newSingerID = 0;
                           int newMusicianID = 0;
                           if (singername == "")
                           {
                               newSingerID = 21;
                           }
                           else
                           {
                               ListenMusic.Singer_ServiceReference.Singer singer = _singer.checkSingerName(singername);
                               if (singer != null)
                               {
                                   newSingerID = singer.SingerID;
                               }
                               else
                               {
                                   Singer newSinger = new Singer();
                                   newSinger.SingerName = singername;
                                   newSinger.Display = false;
                                   newSinger.CountryID = Convert.ToInt16(country);
                                   newSinger.Picture = "no-image.gif";
                                   //int newSingerID = _singer.getMaxID_Singer();
                                   bool rs = _singer.insertNewSinger(newSinger);
                                   if (rs)
                                   {
                                       newSingerID = maxSingerID + 1;
                                   }
                                   else
                                   {
                                       ts.Dispose();
                                   }
                               }
                           }
                           if (musicianname == "")
                           {
                               newMusicianID = 35;
                           }
                           else
                           {
                               ListenMusic.Musician_ServiceReference.Musician musician = _musician.checkMusicianName(musicianname);
                               if (musician != null)
                               {
                                   newMusicianID = musician.MusicianID;
                               }
                               else
                               {
                                   //int newMusicianID = _musician.getMaxID_Musician();
                                   Musician newMusician = new Musician();
                                   newMusician.MusicianName = musicianname;
                                   bool res = _musician.insertNewMusician(newMusician);
                                   if (res)
                                   {
                                       newMusicianID = maxMusician + 1;
                                   }
                                   else
                                   {
                                       ts.Dispose();
                                   }
                               }
                           }
                           foreach (var file in filename)
                           {
                               i++;
                               //string[] array_info_user = (string[])Session["user"];
                               string songurl = Path.GetFileName(file.FileName).ToLower().Replace(" ", "-");
                               string fileName = _upload.RandomStr() + "-" + songurl;
                               string extension = Path.GetExtension(file.FileName);
                               string[] ex_audio = new string[] { ".wma", ".mp3" };
                               string[] ex_video = new string[] { ".mp4", ".wmv", ".flv", ".avi", ".mpeg", ".mpg" };
                               List<string> lst_ex_video = new List<string>();
                               List<string> lst_ex_audio = new List<string>();
                               lst_ex_video.AddRange(ex_video);
                               lst_ex_audio.AddRange(ex_audio);
                               string path = "";
                               ListenMusic.User_Song_ServiceReference.Song s = new ListenMusic.User_Song_ServiceReference.Song();
                               s.SongID = maxSongID + i;
                               s.SongName = songname;
                               //s.MemberID = Convert.ToInt32(array_info_user[1]);
                               s.UserName = "vampire89";
                               s.SongURL = fileName;
                               s.DateUp = DateTime.Now;
                               s.CountView = 0;
                               s.Active = false;
                               s.CategoryID = category;
                               s.CountryID = Convert.ToInt16(country);
                               s.SingerID = newSingerID;
                               s.MusicianID = newMusicianID;
                               if (lyrics.Trim() == "")
                               {
                                   s.Lyrics = null;
                               }
                               else
                               {
                                   s.Lyrics = lyrics;
                               }
                               if (lst_ex_audio.IndexOf(extension) != -1)
                               {
                                   s.TypeID = 1;
                                   path = pathSong + fileName;
                               }
                               if (lst_ex_video.IndexOf(extension) != -1)
                               {
                                   s.TypeID = 2;
                                   path = pathVideo + fileName;

                               }
                               bool result = _upload.uploadSong(s);
                               if (result)
                               {
                                   file.SaveAs(path);                                                                  
                               }
                           }
                           ts.Complete();   
                       }
                       catch (Exception ex)
                       {
                           ts.Dispose();
                           return false;
                           throw ex;
                       }
                   
                   }
               }
               return true;
           }
           catch (FaultException ex)
           {
               return false;
               throw ex;
           }
       }
       public bool InsertNewSinger(HttpPostedFileBase filepicture, string singername, int country, string pathPicture)
       {
           try {
               ListenMusic.Singer_ServiceReference.Singer s = _singer.checkSingerName(singername);
               if (s != null)
               {
                   return false;
               }
               else
               {
                   AdminSinger_ServiceReference.Singer singer = new AdminSinger_ServiceReference.Singer();
                   singer.SingerName = singername;
                   singer.CountryID = (Int16)country;
                   singer.SingerID = _singer.getMaxID_Singer() + 1;
                   singer.Display = false;
                   string path = "";
                   if (filepicture == null)
                   {
                       singer.Picture = "noimage.gif";
                   }
                   else
                   {
                       string fileName = Path.GetFileName(filepicture.FileName).ToLower().Trim().Replace(" ", "-");
                       path = pathPicture + fileName;
                       singer.Picture = fileName;
                   }
                   bool rs = _albumSinger.insertNewSinger(singer);
                   if (rs)
                   {
                       filepicture.SaveAs(path);
                       return true;
                   }
                   else
                   {
                       return false;
                   }
               }
           }
           catch (FaultException e) { return false; throw e; }
       }
    }
}